package file_manager;


import java.io.*;
import java.util.LinkedList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import exception_manager.UncheckedException;



public class HTML_Cleaner {
	private List<String> filePaths;
	
	public HTML_Cleaner(){
		this.filePaths=this.getFilesPath();
	}
	
	
	public static void main(String args[]){
		HTML_Cleaner mf=new HTML_Cleaner();
		mf.initializeXml();
		for(String s:mf.filePaths){
			mf.modify_file(s);
		}
		
	}
	
	private void modify_file(String path) throws UncheckedException{
		String path2;
		try{
			FileReader fr=new FileReader(path);
			BufferedReader br=new BufferedReader(fr);
			path2=path.substring(0,path.length()-4)+"_FN"+".txt";
			FileOutputStream file=new FileOutputStream(path2);
			PrintStream output=new PrintStream(file);
			String s;
			while((s=br.readLine())!=null){
			
				s=this.modify_string(s);
				
//				FileReader fr2=new FileReader("C:/Users/Giorgio/workspace/GIW_progetto_uima/src/main/resources/dictionaries/confronto.txt");
//				BufferedReader br2=new BufferedReader(fr2);
//				String l = br2.readLine();
//				System.out.println(s.length()+" "+ l.length());
//				System.out.println(s.equals(l));
				
				output.println(s);
				
			}
			br.close();
			fr.close();
			output.close();
			file.close();
		}
		catch(Exception e){
			throw new UncheckedException("Error while trying " +
					"to read the file "+path+":\n"+
					e.getMessage());
		}
		this.writeXml(path2);
		System.out.println("File "+path+" modificato.");
	}
	
	private String modify_string(String article){
		//String regex1="[^<!]*?<";
		String regex2="<fb:fan[^>]*>[^<]*</fb:fan>";
		//String regex3="<style.*?>.*?</style>";
		//String regex4="<[^<|^>]*>";
		//String regex5="<.*?>|<.*?>&.*?;";
		//String regex6="  *";
		//article=article.replaceAll(regex2, "");
		article=article.replaceAll(regex2, " ");
		//article=article.replaceAll(regex3, " ");
		//article=article.replaceAll(regex4, " ");
		//article=article.replaceAll(regex5, " ");
		//article=article.replaceAll(regex6, " ");
		
		return article;
	}
	
	
	@SuppressWarnings("unchecked")
	private List<String> getFilesPath() throws 
	UncheckedException{
		List<String> files=new LinkedList<String>();
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(new File("FilePaths.xml"));
			Element root = document.getRootElement();
			List<Element> file =root.getChildren("file");
			List<Element> paths;
			for(Element item:file){
				paths=item.getChildren();
				for(Element path:paths){
					files.add(path.getText());
				}
			}
		} 
		catch (Exception e) {
			throw new UncheckedException("Error while trying " +
					"to read the xml file's paths:\n"+
					e.getMessage());
		}
		return files;
	}
	
	private void writeXml(String path) throws UncheckedException{ 

		try{
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(new File("FilePathsModified.xml"));
			Element rootElement = document.getRootElement();

			Element child1 = new Element("file"); 
			rootElement.addContent(child1); 

			Element child2 = new Element("path"); 
			child2.setText(path); 
			child1.addContent(child2);  

			XMLOutputter outputter = new XMLOutputter(); 
			outputter.output(document, new FileOutputStream("FilePathsModified.xml"));
		}
		catch(Exception e){
			throw new UncheckedException("Error while trying to write the xml file: "+e.getMessage());
		}
	}
	
	private void initializeXml() throws UncheckedException{
		Element rootElement = new Element("data"); 
		Document document = new Document(rootElement);
		XMLOutputter outputter = new XMLOutputter();
		try{
		outputter.output(document, new FileOutputStream("FilePathsModified.xml"));
		}
		catch(Exception e){
			throw new UncheckedException("Error while trying to create the xml file: "+e.getMessage());
		}
		
	}


}

